home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / xstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  2.8 KB  |  159 lines

  1. /* xstack.c -- Simple stacks management file.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #include <sys/types.h>
  35.  
  36. #include "xmalloc.h"
  37. #include "xstring.h"
  38. #include "xstack.h"
  39.  
  40.  
  41. /*
  42.  * Create a new stack.
  43.  */
  44.  
  45. xstack_t *
  46. xstack_init(esize)
  47.     int esize;
  48. {
  49.     xstack_t *s = (xstack_t *)xcalloc(1, sizeof(xstack_t));
  50.  
  51.     s->esize = esize;
  52.     return s;
  53. }
  54.  
  55.  
  56. /*
  57.  * Destroy a stack.
  58.  */
  59.  
  60. void
  61. xstack_end(stack)
  62.     xstack_t *stack;
  63. {
  64.     if (stack == NULL)
  65.     return;
  66.  
  67.     if (stack->data)
  68.     xfree(stack->data);
  69.  
  70.     xfree(stack);
  71. }
  72.  
  73.  
  74. /*
  75.  * Push one element to a stack.
  76.  */
  77.  
  78. void
  79. xstack_push(stack, data)
  80.     xstack_t *stack;
  81.     void *data;
  82. {
  83.     stack->data = (void *)xrealloc(stack->data, ++stack->point * stack->esize);
  84.  
  85.     memcpy(((char *)stack->data) + (stack->point - 1) * stack->esize,
  86.        data,
  87.        stack->esize);
  88. }
  89.  
  90.  
  91. /*
  92.  * Pop one element from a stack.
  93.  */
  94.  
  95. void *
  96. xstack_pop(stack, data)
  97.     xstack_t *stack;
  98.     void *data;
  99. {
  100.     if (stack->point == 0)
  101.     return NULL;
  102.  
  103.     memcpy(data,
  104.        ((char *)stack->data) + --stack->point * stack->esize,
  105.        stack->esize);
  106.  
  107.     stack->data = (void *)xrealloc(stack->data, stack->point * stack->esize);
  108.  
  109.     return data;
  110. }
  111.  
  112.  
  113. /*
  114.  * Preview the stack element located at 'offset' elements from the stack
  115.  * point.  The stack point is located at offset = 0.
  116.  */
  117.  
  118. void *
  119. xstack_preview(stack, data, offset)
  120.     xstack_t *stack;
  121.     void *data;
  122.     int offset;
  123. {
  124.     if (stack->point == 0 || offset > stack->point)
  125.     return NULL;
  126.  
  127.     memcpy(data,
  128.        ((char *)stack->data) + (stack->point - offset) * stack->esize,
  129.        stack->esize);
  130.  
  131.     return data;
  132. }
  133.  
  134.  
  135. /*
  136.  * Truncate a stack to 'point' elements.
  137.  */
  138.  
  139. void
  140. xstack_truncate(stack, point)
  141.     xstack_t *stack;
  142.     int point;
  143. {
  144.     stack->point = point;
  145.     stack->data = (void *)xrealloc(stack->data, stack->point * stack->esize);
  146. }
  147.  
  148.  
  149. /*
  150.  * Return the stack point.
  151.  */
  152.  
  153. int
  154. xstack_point(stack)
  155.     xstack_t *stack;
  156. {
  157.     return stack->point;
  158. }
  159.